package com.student.work.controller;

import com.student.work.common.BaseResponse;
import com.student.work.model.JobApplication;
import com.student.work.model.JobInfo;
import com.student.work.model.UserAttendance;
import com.student.work.model.UserSalaryDetail;
import com.student.work.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @date 2020/3/3 20:22
 */
@Controller
@RequestMapping("/work")
public class JobController {

    @Autowired
    JobService jobService;

    /**
     * 新增工作
     */
    @PostMapping("/addJob")
    @ResponseBody
    public BaseResponse<Map<String, Object>> addJob(@RequestBody JobInfo jobInfo) {
        return jobService.addJob(jobInfo);
    }

    /**
     * 获取工作详情
     */
    @GetMapping("/getJob")
    @ResponseBody
    public BaseResponse<JobInfo> getJob(@RequestParam("jobId") Long jobId) {
        return jobService.getJob(jobId);
    }

    /**
     * 获取工作列表
     *
     * @param pageNum       页码
     * @param pageSize      页面数据量
     * @param jobKeyword    搜索工作标题关键字
     * @param departKeyword 搜索工作部门关键字
     * @param date          搜索时间范围
     * @param userId        用户ID（用工单位查看自己发布的工作时传的）
     */
    @GetMapping("/getJobList")
    @ResponseBody
    public BaseResponse<Map<String, Object>> getJobList(@RequestParam("pageNum") int pageNum,
                                                        @RequestParam("pageSize") int pageSize,
                                                        @RequestParam(value = "jobKeyword", required = false) String jobKeyword,
                                                        @RequestParam(value = "departKeyword", required = false) String departKeyword,
                                                        @RequestParam(value = "isRecommend", required = false) Integer isRecommend,
                                                        @RequestParam(value = "date", required = false) Integer date,
                                                        @RequestParam(value = "userId", required = false) Long userId) {
        return jobService.getJobList(pageNum, pageSize, jobKeyword, departKeyword, isRecommend, date, userId);

    }

    /**
     * 申请工作
     */
    @PostMapping("/applyJob")
    @ResponseBody
    public BaseResponse<Map<String, Object>> applyJob(@RequestBody JobApplication application) {
        return jobService.applyJob(application);
    }

    /**
     * 学生：我的申请
     * 我的工作，已经通过申请的，applyStatus=2
     */
    @GetMapping("/getListByUserId")
    @ResponseBody
    public BaseResponse<Map<String, Object>> getListByUserId(@RequestParam("pageNum") int pageNum,
                                                             @RequestParam("pageSize") int pageSize,
                                                             @RequestParam("userId") Long userId,
                                                             @RequestParam(value = "applyStatus", required = false) Integer applyStatus) {
        return jobService.getListByUserId(pageNum, pageSize, userId, applyStatus);
    }

    /**
     * 用工单位：我的申请
     */
    @GetMapping("/getApplyByUserId")
    @ResponseBody
    public BaseResponse<Map<String, Object>> getApplyByUserId(@RequestParam("pageNum") int pageNum,
                                                              @RequestParam("pageSize") int pageSize,
                                                              @RequestParam("userId") Long userId,
                                                              @RequestParam(value = "applyStatus", required = false) Integer applyStatus) {
        return jobService.getApplyByUserId(pageNum, pageSize, userId, applyStatus);
    }

    /**
     * 修改申请状态
     */
    @PostMapping("changeApply")
    @ResponseBody
    public BaseResponse<Map<String, Object>> changeApply(@RequestBody JobApplication apply) {
        return jobService.changeApply(apply);
    }

    /**
     * 上下班打卡
     */
    @PostMapping("createAttendance")
    @ResponseBody
    public BaseResponse<Map<String, Object>> createAttendance(@RequestBody UserAttendance attendance,
                                                              @RequestParam("type") Integer type) {
        return jobService.createAttendance(attendance, type);
    }

    /**
     * 获取打卡记录
     */
    @GetMapping("/getAttendanceList")
    @ResponseBody
    public BaseResponse<Map<String, Object>> getAttendanceList(@RequestParam("pageNum") int pageNum,
                                                               @RequestParam("pageSize") int pageSize,
                                                               @RequestParam("userId") Long userId,
                                                               @RequestParam("jobId") Long jobId) {
        return jobService.getAttendanceList(pageNum, pageSize, userId, jobId);
    }

    /**
     * 获取薪资记录
     */
    @GetMapping("/getSalaryList")
    @ResponseBody
    public BaseResponse<Map<String, Object>> getSalaryList(@RequestParam("pageNum") int pageNum,
                                                           @RequestParam("pageSize") int pageSize,
                                                           @RequestParam("userId") Long userId,
                                                           @RequestParam("jobId") Long jobId) {
        return jobService.getSalaryList(pageNum, pageSize, userId, jobId);
    }

    /**
     * 发放薪资
     */
    @PostMapping("/payOff")
    @ResponseBody
    public BaseResponse<Map<String, Object>> payOff(@RequestBody UserSalaryDetail detail) {
        return jobService.payOff(detail);
    }

    /**
     * 删除工作
     */
    @PostMapping("/deleteJob")
    @ResponseBody
    public BaseResponse<Map<String, Object>> deleteJob(@RequestBody JobInfo job) {
        return jobService.deleteJob(job);
    }

    @GetMapping("/getAttendance")
    @ResponseBody
    public BaseResponse<Map<String, Object>> getAttendance(@RequestParam("jobId") Long jobId,
                                                           @RequestParam("studentId") Long studentId) {
        return jobService.getAttendance(jobId, studentId);
    }
}
